﻿Namespace Framework
    Public Class StickyBindingExtension
        Inherits System.Windows.Markup.MarkupExtension

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal path As String)
            MyBase.New()
            Me.Path = path
        End Sub

        Private _Path As String
        Public Property Path() As String
            Get
                Return _Path
            End Get
            Set(ByVal value As String)
                _Path = value
            End Set
        End Property

        Private _Mode As BindingMode = BindingMode.Default
        Public Property Mode() As BindingMode
            Get
                Return _Mode
            End Get
            Set(ByVal value As BindingMode)
                _Mode = value
            End Set
        End Property

        Private _Converter As IValueConverter
        Public Property Converter() As IValueConverter
            Get
                Return _Converter
            End Get
            Set(ByVal value As IValueConverter)
                _Converter = value
            End Set
        End Property

        Private _ConverterParameter As Object
        Public Property ConverterParameter() As Object
            Get
                Return _ConverterParameter
            End Get
            Set(ByVal value As Object)
                _ConverterParameter = value
            End Set
        End Property

        Private _ConverterCulture As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture
        Public Property ConverterCulture() As System.Globalization.CultureInfo
            Get
                Return _ConverterCulture
            End Get
            Set(ByVal value As System.Globalization.CultureInfo)
                _ConverterCulture = value
            End Set
        End Property

        Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
            Dim target As System.Windows.Markup.IProvideValueTarget = serviceProvider.GetService(GetType(System.Windows.Markup.IProvideValueTarget))
            If target IsNot Nothing Then
                If TypeOf target.TargetProperty Is DependencyProperty Then
                    If TypeOf target.TargetObject Is StickyViewModel Then
                        Dim dataPath As String = "DataContext"
                        If Not String.IsNullOrEmpty(Path) Then
                            If Path.StartsWith("/") Or Path.StartsWith("[") Then
                                dataPath &= Path
                            Else
                                dataPath &= "." & Path
                            End If
                        End If
                        Dim b As New Binding(dataPath)
                        b.Source = target.TargetObject
                        b.Mode = Mode
                        b.Converter = Converter
                        b.ConverterCulture = ConverterCulture
                        b.ConverterParameter = ConverterParameter
                        Return b.ProvideValue(serviceProvider)
                    End If
                End If
            End If
            Return Me
        End Function
    End Class
End Namespace